package com.sina.microblog.ut.cases
{
	import com.sina.microblog.MicroBlog;
	import com.sina.microblog.events.MicroBlogErrorEvent;
	import com.sina.microblog.events.MicroBlogEvent;
	import com.sina.microblog.ut.utils.AssertResult;
	import com.sina.microblog.ut.utils.ResultDataUtils;
	import com.sina.microblog.ut.utils.URLRequestChecker;
	import com.sina.microblog.ut.utils.URLRequestParameters;
	import com.sina.microblog.utils.StringEncoders;
	import com.ut.nm_test_internal;
	
	import flash.net.URLRequest;
	import flash.net.URLRequestHeader;
	
	import org.flexunit.asserts.assertEquals;
	import org.flexunit.asserts.assertNotNull;
	import org.flexunit.asserts.assertTrue;
	import org.flexunit.async.Async;

	use namespace nm_test_internal;
	
	public class OAuthTest
	{
		private static const PUBLIC_TIMELINE_URL:String = "http://api.t.sina.com.cn/statuses/public_timeline.xml";
		private static const FRIENDS_TIMELINE_URL_BASE:String = "http://api.t.sina.com.cn/statuses/friends_timeline.xml";
		private static const COUNTS_URL_BASE:String = "http://api.t.sina.com.cn/statuses/counts.xml";
		
		private static const TIME_OUT:uint = 500;	
		private static var mb:MicroBlog;
		private static var origString:String = "asdfadsf dfd";
		private static var encodeString:String = StringEncoders.urlEncodeUtf8String(origString);
		
		public function OAuthTest()
		{
		}
		
		[Before]
		public function setUp():void
		{
			if ( null == mb )
			{
				mb = new MicroBlog();
				mb.accessTokenKey = "sadfadsfasdf";
				mb.accessTokenSecrect = "adfljasdlfjoiejflasjdfl";
				mb.consumerKey = "sdfjalsdjflsdjf";
				mb.consumerSecret = "oiejlajsdfjoejasfjejlfkjdklfj";
			}
		}
		[After]
		public function cleanUp():void
		{
			mb = null;	
		}
		// Public timeline
		[Test(async,timeout="20000")]
		public function testPublicTimeline():void
		{
			Async.handleEvent(this, mb, MicroBlogEvent.LOAD_PUBLIC_TIMELINE_RESULT, AssertResult.assertStatusArray, TIME_OUT);
			var params:URLRequestParameters = new URLRequestParameters(StringEncoders.urlEncodeUtf8String( mb.source));
			var url:String = PUBLIC_TIMELINE_URL + params.toGetString();
			var req:URLRequest = new URLRequest(url);
			mb.loadPublicTimeline();
			URLRequestChecker.check(mb, req, ResultDataUtils.statusArray);
		}
		[Test(async,timeout="20000")]
		public function testPublicTimelineFail():void
		{
			Async.handleEvent(this, mb, MicroBlogErrorEvent.LOAD_PUBLIC_TIMELINE_ERROR, AssertResult.assertError, TIME_OUT);
			var params:URLRequestParameters = new URLRequestParameters(StringEncoders.urlEncodeUtf8String( mb.source));
			var url:String = PUBLIC_TIMELINE_URL + params.toGetString();
			var req:URLRequest = new URLRequest(url);
			mb.loadPublicTimeline();
			URLRequestChecker.check(mb, req, ResultDataUtils.error);
			
			
		}
		
		//Friends timeline
		[Test(async,timeout="20000")]
		public function testFriendsTimelineWithoutParams():void
		{
			var params:URLRequestParameters = new URLRequestParameters(StringEncoders.urlEncodeUtf8String( mb.source));
			var url:String = FRIENDS_TIMELINE_URL_BASE + params.toGetString();
			Async.handleEvent(this, mb, MicroBlogEvent.LOAD_FRIENDS_TIMELINE_RESULT, AssertResult.assertStatusArray, TIME_OUT);
			
			var req:URLRequest = new URLRequest(url);
			mb.loadFriendsTimeline();
			URLRequestChecker.check(mb, req, ResultDataUtils.statusArray);			
			
		}
		[Test(async,timeout="20000")]
		public function testFriendsTimelineFail():void
		{
			var params:URLRequestParameters = new URLRequestParameters(StringEncoders.urlEncodeUtf8String( mb.source));
			var url:String = FRIENDS_TIMELINE_URL_BASE + params.toGetString();
			Async.handleEvent(this, mb, MicroBlogErrorEvent.LOAD_FRIENDS_TIMELINE_ERROR, AssertResult.assertError, TIME_OUT);
			
			var req:URLRequest = new URLRequest(url);
			mb.loadFriendsTimeline();
			URLRequestChecker.check(mb, req, ResultDataUtils.error);			
			
		}
		
		//Test Counts.
		[Test(async,timeout="20000")]
		public function testCounts():void
		{
			var ids:Array = [1,233,4555,64545];
			var idStr:String = ids.join(",");
			var params:URLRequestParameters = new URLRequestParameters(StringEncoders.urlEncodeUtf8String( mb.source));
			params.addParam("ids", StringEncoders.urlEncodeUtf8String(idStr));
			var url:String =COUNTS_URL_BASE + params.toGetString();
			Async.handleEvent(this, mb, MicroBlogEvent.LOAD_STATUS_COUNTS_RESULT, AssertResult.assertCountArray, TIME_OUT);
			
			var req:URLRequest = new URLRequest(url);
			mb.loadStatusCounts(ids);
			URLRequestChecker.check(mb, req, ResultDataUtils.counts);			
			
		}
		[Test(async,timeout="20000")]
		public function testCountsFail():void
		{
			var ids:Array = [1,233,4555,64545];
			var idStr:String = ids.join(",");
			var params:URLRequestParameters = new URLRequestParameters(StringEncoders.urlEncodeUtf8String( mb.source));
			params.addParam("ids", StringEncoders.urlEncodeUtf8String(idStr));
			var url:String =COUNTS_URL_BASE + params.toGetString();
			Async.handleEvent(this, mb, MicroBlogErrorEvent.LOAD_STATUS_COUNTS_ERROR, AssertResult.assertError, TIME_OUT);
			
			var req:URLRequest = new URLRequest(url);
			mb.loadStatusCounts(ids);
			URLRequestChecker.check(mb, req, ResultDataUtils.error);			
			
		}
		
		[Test]
		public function testSetSource():void
		{
			mb.source = origString;
			assertEquals(mb.source, encodeString);
		}

		[Test]
		public function testAccessTokenKey():void
		{
			var ck:String = "adfadfadsfg";
			
			mb.accessTokenKey = ck;
			assertEquals(mb.accessTokenKey, ck);
			mb.accessTokenKey = null;
			assertEquals(mb.accessTokenKey, "");
		}
		[Test]
		public function testAccessTokenSecret():void
		{
			var cs:String = "adfadfadsfg";
			
			mb.accessTokenSecrect = cs;
			assertEquals(mb.accessTokenSecrect, cs);
			mb.accessTokenSecrect = null;
			assertEquals(mb.accessTokenSecrect, "");
		}
		[Test]
		public function testSetEmptyComsumerKey():void
		{
			mb.consumerKey = null; 
			mb.loadPublicTimeline();
			var req:URLRequest = mb.lastRequest;
			assertNotNull(req);
			assertEquals(req.requestHeaders.length, 1);
			var s:String = (req.requestHeaders[0] as URLRequestHeader).value;
			assertTrue( s.search('oauth_consumer_key=""')>0);
		
			mb.lastRequest = null;
		}
		[Test]
		public function testSetPin():void
		{
			var pin:String = "abcdef";
			mb.pin = pin;
			
		}
	}
}